import stomp
from config import config
import time



class Worker(stomp.ConnectionListener):
    def __init__(self):
        self.username = config.get("username")
        self.password = config.get("password")
        self.host = config.get("host")
        self.client_id = config.get("client_id")
        self.i600 = config.get("destination") # i600队列
        self.apm = config.get("source") # APM队列
        self.conn = None # 连接器

    def on_message(self, frame):
        print('Received a message: "{}"'.format(frame.body))
        # 发送数据到i6000
        self.conn.send(body=frame.body + "-i600", destination=self.i600)


    def on_error(self, frame):
        print('Received an error "{}"'.format(frame.body))

    def __create_con(self):
        self.conn = stomp.Connection(self.host)
        self.conn.set_listener('', self)
        self.conn.connect(self.username, self.password, wait=True)

    def read_data(self):
        self.__create_con()

        self.conn.subscribe(destination=self.apm, id=1, ack='auto')

        while 1:
            self.conn.send(body='Hello, ActiveMQ!', destination=self.apm)
            time.sleep(2)
            pass


    def __del__(self):
        # 断开连接
        self.conn.disconnect()



    def run(self):
        self.read_data()



if __name__ == '__main__':
    worker = Worker()

    worker.run()